<?php
	class Users_model extends MY_Model{
		
		public function __construct(){
			parent::__construct();
		}
		
		public function login($username, $password){
			$this->db->select("id, username, password, gid");
			$this->db->from('users');
			$this->db->where('username = '.$this->db->escape($username));
			$this->db->where('password = '.$this->db->escape(MD5($password)));
			
			$query = $this->db->get();
			if($query->num_rows()==1)
				return $query->result();
			else
				return false;
		}
		
		public function getUserList($id = FALSE){			
			$this->db->select('u.*, g.name AS g_name');
			$this->db->from('users u');
			$this->db->join('group g', 'u.gid = g.id', 'left');
			
			if($id!=FALSE){				
				$this->db->where('u.id = '.$id, NULL, FALSE);
				$query = $this->db->get();	
				return $query->row_array();
			}
			else{
				$this->db->limit($this->record_per_page, $this->limit);
				$query = $this->db->get();	
				return $query->result_array();
			}				
		}
		
		public function get_total(){
			$this->db->select('*');
			$this->db->from('users');			
			$query = $this->db->get();					
			return $query->num_rows();
		}
		
		public function save(){
			$id = $this->input->post('id');
			$data = array(
				"username" => $this->input->post('username'),				
				"fname"		=> $this->input->post('lname'),
				"lname"		=> $this->input->post('fname'),
				"email"		=> $this->input->post('email'),
				"status"	=> $this->input->post('status'),
				"gid"		=> $this->input->post('gid')
			);
			if(empty($id)){
				$data['password'] = MD5($this->input->post('password'));
				return $this->db->insert('users', $data);
			}
			else{
				$this->db->where('id', $id);
				return $this->db->update('users', $data);
			}
		}
		
		public function update_password(){
			$id = $this->input->post('id');
			$data['password'] = MD5($this->input->post('password'));
			$this->db->where('id', $id);
			return $this->db->update('users', $data);
		}
		
		public function delete($id){
			$this->db->where('id', $id);
			$this->db->delete('users');
		}
		
		public function checkPassword(){
			$password 			= $this->input->post('password');
			$password_confirm 	= $this->input->post('password_confirm');
			return ($password == $password_confirm) ? TRUE : FALSE;
		}
		
		public function checkDuplicateUsername(){
			$id = $this->input->post('id');
			$this->db->select("username");
			$this->db->from('users');
			$this->db->where('username = '.$this->db->escape($this->input->post('username')));
			if(!empty($id) && $id!=0)
				$this->db->where('id <> '.$id);
			$query = $this->db->get();
			return ($query->num_rows()==1) ? FALSE : TRUE;
			
		}
		
		public function checkDuplicateEmail(){
			$id = $this->input->post('id');
			$this->db->select("email");
			$this->db->from('users');
			$this->db->where('email = '.$this->db->escape($this->input->post('email')));
			if(!empty($id) && $id!=0)
				$this->db->where('id <> '.$id);
			$query = $this->db->get();
			return ($query->num_rows()==1) ? FALSE : TRUE;
		}
		
		
		
	}
?>